package cn.caishen.report.service.report.impl;

import cn.caishen.report.common.exceptions.BusinessException;
import cn.caishen.report.common.utils.LbMap;
import cn.caishen.report.dao.report.ReportFieldDao;
import cn.caishen.report.domain.report.ReportField;
import cn.caishen.report.service.report.ReportFieldService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 报表字段配置Service实现类
 *
 * @author LB
 * @description 报表字段配置Service实现类
 * @create 2022-01-17 15:55:40
 */
@Service("reportFieldService")
@Transactional(rollbackFor = Throwable.class)
public class ReportFieldServiceImpl extends ServiceImpl<ReportFieldDao, ReportField> implements ReportFieldService {

    private final ReportFieldDao reportFieldDao;

    public ReportFieldServiceImpl(ReportFieldDao reportFieldDao) {
        this.reportFieldDao = reportFieldDao;
    }

    /**
     * 分页查询列表
     *
     * @param page  页数
     * @param limit 条数
     * @param param 参数
     * @return PageInfo 分页列表
     */
    @Override
    public PageInfo<ReportField> getList(int page, int limit, LbMap param) {
        QueryWrapper<ReportField> wrapper = new QueryWrapper<>();
        PageHelper.startPage(page, limit);
        List<ReportField> list = reportFieldDao.selectList(wrapper);
        return new PageInfo<ReportField>(list);
    }

    /**
     * 查询所有列表
     *
     * @param param 参数
     * @return list 全部列表
     */
    @Override
    public List<ReportField> getAllList(LbMap param) {
        QueryWrapper<ReportField> wrapper = new QueryWrapper<>();
        return reportFieldDao.selectList(wrapper);
    }

    /**
     * 通过编号查询
     *
     * @param primaryKey
     * @return 报表字段配置
     */
    @Override
    @SneakyThrows
    public ReportField getById(String primaryKey) {
        ReportField reportField = reportFieldDao.selectById(primaryKey);
        if (Objects.isNull(reportField)) {
            throw new BusinessException("报表字段配置编号不正确");
        }
        return reportField;
    }

    /**
     * 新增信息
     *
     * @param reportField reportField
     */
    @Override
    @SneakyThrows
    public void saveOne(ReportField reportField) {
        int count = reportFieldDao.insert(reportField);
        if (count != 1) {
            throw new BusinessException("报表字段配置保存失败");
        }
    }

    /**
     * 更新信息
     *
     * @param reportField reportField
     */
    @Override
    @SneakyThrows
    public void update(ReportField reportField) {
        int count = reportFieldDao.updateById(reportField);
        if (count != 1) {
            throw new BusinessException("报表字段配置更新失败");
        }
    }

    /**
     * 删除信息
     *
     * @param primaryKey id
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        ReportField reportField = reportFieldDao.selectById(primaryKey);
        if (Objects.isNull(reportField)) {
            throw new BusinessException("未通过编号找到报表字段配置");
        }
        int count = reportFieldDao.deleteById(primaryKey);
        if (count != 1) {
            throw new BusinessException("报表字段配置删除失败");
        }
    }
}
